package src.leetcode.everyday.mid_lcr081;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 李逸重
 * @CreateTime: 2025-08-29
 * @Description:
 * @Version: 1.0
 */
public class combinationSum {
    public static void main(String[] args) {
        System.out.println(new Solution().combinationSum(new int[]{2, 3, 6, 7}, 7));
    }
}


class Solution {
    int[] candidates;
    int target;
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        this.candidates = candidates;
        this.target = target;
        dfs(0, 0, new ArrayList<>());
        return res;
    }

    void dfs(int index, int sum, List<Integer> path) {
        if (sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }
        if (sum > target) return;

        for (int i = index; i < candidates.length; i++) {
            sum += candidates[i];
            path.add(candidates[i]);
            dfs(i, sum, path);
            sum -= candidates[i];
            path.remove(path.size() - 1);
        }
    }
}